#! /bin/bash
log_level=0 # 0 debug, 1 info, 2 warn, 3 error
log_file=$0".log"

function log() {
    local msg; local log_type
    log_type=$1
    msg=$2
    date_time=$(date "+%D %T")
    log_format="[${log_type}]\t${date_time}\t function_name:${FUNCNAME[*]/log/}\t[line:$(caller 0 | awk '{print $1}')]\t${msg}"
    {
      case ${log_type} in
      debug)
        [[ $log_type -le 0 ]] && echo -e "\033[30m${log_format}\033[0m"
        ;;
      info)
        [[ $log_type -le 1 ]] && echo -e "\033[32m${log_format}\033[0m"
        ;;
      warn)
        [[ $log_type -le 2 ]] && echo -e "\033[33m${log_format}\033[0m"
        ;;
      error)
        [[ $log_type -le 3 ]] && echo -e "\033[31m${log_format}\033[0m"
        ;;
      esac
    } | tee -a "$log_file"
}

function debug() {
    log debug "$1"
}

function info() {
    log info "$1"
}

function warn() {
    log warn "$1"
}

function error() {
    log error "$1"
}